package com.ruoyi.dora.portal.controller;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.dora.portal.entity.SysMenu;
import com.ruoyi.dora.portal.entity.SysUser;
import com.ruoyi.dora.portal.service.ISysConfigService;
import com.ruoyi.dora.portal.service.ISysMenuService;
import com.ruoyi.dora.portal.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.SubjectContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author lvzihai
 * @date 2021/7/22
 **/
@Slf4j
@Controller
public class PortalController {

  @Autowired
  private ISysUserService sysUserService;

  @Autowired
  private ISysMenuService menuService;

  @Autowired
  private ISysConfigService configService;

  @GetMapping({"/","/index"})
  public String index(ModelMap modelMap){

    Object principal = SecurityUtils.getSubject().getPrincipal();
    log.info("principal {}",principal.toString());
    SysUser user = sysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getLoginName, "admin"), false);
    List<SysMenu> menus = menuService.selectMenusByUser(user);
    modelMap.put("menus", menus);
    modelMap.put("user", user);
    modelMap.put("sideTheme", configService.selectConfigByKey("sys.index.sideTheme").getConfigValue());
    modelMap.put("skinName", configService.selectConfigByKey("sys.index.skinName").getConfigValue());
    modelMap.put("ignoreFooter", configService.selectConfigByKey("sys.index.ignoreFooter").getConfigValue());
//    modelMap.put("copyrightYear", RuoYiConfig.getCopyrightYear());
//    modelMap.put("demoEnabled", RuoYiConfig.isDemoEnabled());
//    modelMap.put("isDefaultModifyPwd", initPasswordIsModify(user.getPwdUpdateDate()));
//    modelMap.put("isPasswordExpired", passwordIsExpiration(user.getPwdUpdateDate()));
    modelMap.put("copyrightYear", "2021");
    modelMap.put("demoEnabled", "true");
    modelMap.put("isDefaultModifyPwd", false);
    modelMap.put("isPasswordExpired", false);

    // 菜单导航显示风格
//    String menuStyle = configService.selectConfigByKey("sys.index.menuStyle");
    String menuStyle = "default";
    // 移动端，默认使左侧导航菜单，否则取默认配置
    String indexStyle = menuStyle;//ServletUtils.checkAgentIsMobile(ServletUtils.getRequest().getHeader("User-Agent")) ? "index" : menuStyle;

    // 优先Cookie配置导航菜单
//    Cookie[] cookies = ServletUtils.getRequest().getCookies();
//    for (Cookie cookie : cookies)
//    {
//      if (StringUtils.isNotEmpty(cookie.getName()) && "nav-style".equalsIgnoreCase(cookie.getName()))
//      {
//        indexStyle = cookie.getValue();
//        break;
//      }
//    }
    if("topnav".equalsIgnoreCase(indexStyle)){
      return "index-topnav";
    }
    return "index";
  }


  @GetMapping("/system/main")
  public String sysMain(Model model){
    model.addAttribute("version","0.0.1");
    return "main";
  }
}
